import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class ChineseCheck extends JFrame{

	private static final long serialVersionUID = 1L;

	Board br= new Board();
	DrawGame dg=new DrawGame(br);
	int turn=0;



	int nbrhuman=2;
	int turnhuman=0;
	int nbrpair=1;
	public static void main(String[] args) {

		new ChineseCheck(2,1);
	}

	public ChineseCheck(int human,int nbpair) {
		super("chinese checker");
		nbrhuman=human;
		nbrpair=nbpair;

		setSize(900, 700);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		br.generateBoard();
		br.generatePlacesGraph();

		for (int i=0;i<nbrpair;i++){
			br.players.add(new Player(i,br));
			br.players.add(new Player(i+3,br));

		}



		br.player=br.players.get(turn);

		//each players give their position
		for (int i=0;i<br.players.size();i++)
			br.loadfreeplace(br.players.get(i));


		dg.addMouseListener(new MouseListener()  {

			public void mouseReleased(MouseEvent e) {
				
			
			
				
			}
			public void mousePressed(MouseEvent e) {


				int dx,dy;

				for (int i=0;i<121;i++){

					dx=br.getPlace(i).x-e.getX();
					dy=br.getPlace(i).y-e.getY();
					if(Math.sqrt(dx*dx+dy*dy)<br.stoneSize/2){

						if(br.player.isMyStone(i))	{
							br.selectedStone=i; 
							//System.out.println(br.player.position);
						}else{
							if(br.selectedStone>-1){
								//bot versus human
								if(br.player.move(br.selectedStone,i)){
									br.countTurn++;
									br.selectedStone=-1;
									//System.out.println("human play"+br.player.position);
									dg.RotationAnimation(4);
									changeTurn();
	//								turnhuman++;
									/*
									if(turnhuman==nbrhuman){
										for (int k=0;k<nbrpair*2-nbrhuman;k++){
											//System.out.println("bot play"+br.player.position);
											waitforbot2();
											changeTurn();
										}
										turnhuman=0;
									}
*/

								}


							}

							br.selectedStone=-1;

							//br.players.mystone
						}

					
						dg.repaint();

						break;
					}
				}
			}

			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});


		add(dg);
		setVisible(true);



/*
		for(int i=0;(i<5000);i++){

			waitforbot2();
			//System.out.println(br.player.position+" :"+br.player.CalculDistanceFin());
		//	if(br.player.CalculDistanceFin()==120)break;
			changeTurn();
			waitforbot2();
			//System.out.println(br.player.position+" :"+br.player.CalculDistanceFin());
		//	if(br.player.CalculDistanceFin()==120)break;
			changeTurn();
		}
*/	System.out.println(br.countTurn);


	}


	public void waitforbot() {

		int[] res=new SampleRobot().nextMove(this.br,br.player);
		br.player.move(res[0],res[1]);

	}
	public void waitforbot2() {

		int[] res=new SampleRobot2().nextMove(this.br,br.player);
		br.player.move(res[0],res[1]);

	}
	private void changeTurn(){
		turn=(turn+1)%br.players.size();	
		br.player=br.players.get(turn);
		dg.repaint();
		if(turn==0)br.countTurn++;

/*
		try {

			Thread.sleep(10);
		} catch (InterruptedException e1) {e1.printStackTrace();}
*/
	}



}
